package org.mat.ipaas.ucenter.bff.admin.modules.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.api.system.SysCategoryRestAPI;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysCategoryDTO;
import org.mat.ipaas.ucenter.service.dto.system.model.TreeSelectModelDTO;
import org.mat.ipaas.ucenter.service.dto.system.vo.DictModelDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;

/**
 * 分类字典
 */
@RestController
@RequestMapping("/sys/category")
@Slf4j
public class SysCategoryController {

    @Autowired
    private SysCategoryRestAPI sysCategoryRestAPI;

    /**
     * 分页列表查询
     *
     * @param sysCategoryDTO
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/rootList")
    public MatApiResponse<Page<SysCategoryDTO>> queryPageList(SysCategoryDTO sysCategoryDTO,
                                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        MatApiResponse<Page<SysCategoryDTO>> matApiResponse = sysCategoryRestAPI.queryPageList(sysCategoryDTO, pageNo, pageSize);
        return matApiResponse;
    }

    @GetMapping(value = "/childList")
    public MatApiResponse<List<SysCategoryDTO>> queryPageList(SysCategoryDTO sysCategoryDTO) {
        MatApiResponse<List<SysCategoryDTO>> matApiResponse = sysCategoryRestAPI.queryPageList(sysCategoryDTO);
        return matApiResponse;
    }


    /**
     * 添加
     *
     * @param sysCategoryDTO
     * @return
     */
    @PostMapping(value = "/add")
    public MatApiResponse<SysCategoryDTO> add(@RequestBody SysCategoryDTO sysCategoryDTO) {
        MatApiResponse<SysCategoryDTO> matApiResponse = sysCategoryRestAPI.add(sysCategoryDTO);
        return matApiResponse;
    }

    /**
     * 编辑
     *
     * @param sysCategoryDTO
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public MatApiResponse<SysCategoryDTO> edit(@RequestBody SysCategoryDTO sysCategoryDTO) {
        MatApiResponse<SysCategoryDTO> matApiResponse = sysCategoryRestAPI.edit(sysCategoryDTO);
        return matApiResponse;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public MatApiResponse<SysCategoryDTO> delete(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<SysCategoryDTO> matApiResponse = sysCategoryRestAPI.delete(id);
        return matApiResponse;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    public MatApiResponse<SysCategoryDTO> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        MatApiResponse<SysCategoryDTO> matApiResponse = sysCategoryRestAPI.deleteBatch(ids);
        return matApiResponse;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public MatApiResponse<SysCategoryDTO> queryById(@RequestParam(name = "id", required = true) String id) {
        MatApiResponse<SysCategoryDTO> matApiResponse = sysCategoryRestAPI.queryById(id);
        return matApiResponse;
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(SysCategoryDTO sysCategoryDTO) {
        ModelAndView modelAndView = sysCategoryRestAPI.exportXls(sysCategoryDTO);
        return modelAndView;
    }

    /**
     * 通过excel导入数据
     *
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public MatApiResponse<?> importExcel() throws IOException {
        MatApiResponse<?> matApiResponse = sysCategoryRestAPI.importExcel();
        return matApiResponse;
    }


    /**
     * 加载单个数据 用于回显
     */
    @RequestMapping(value = "/loadOne", method = RequestMethod.GET)
    public MatApiResponse<SysCategoryDTO> loadOne(@RequestParam(name = "field") String field, @RequestParam(name = "val") String val) {
        MatApiResponse<SysCategoryDTO> matApiResponse = sysCategoryRestAPI.loadOne(field, val);
        return matApiResponse;
    }

    /**
     * 加载节点的子数据
     */
    @RequestMapping(value = "/loadTreeChildren", method = RequestMethod.GET)
    public MatApiResponse<List<TreeSelectModelDTO>> loadTreeChildren(@RequestParam(name = "pid") String pid) {
        MatApiResponse<List<TreeSelectModelDTO>> matApiResponse = sysCategoryRestAPI.loadTreeChildren(pid);
        return matApiResponse;
    }

    /**
     * 加载一级节点/如果是同步 则所有数据
     */
    @RequestMapping(value = "/loadTreeRoot", method = RequestMethod.GET)
    public MatApiResponse<List<TreeSelectModelDTO>> loadTreeRoot(@RequestParam(name = "async") Boolean async, @RequestParam(name = "pcode") String pcode) {
        MatApiResponse<List<TreeSelectModelDTO>> matApiResponse = sysCategoryRestAPI.loadTreeRoot(async, pcode);
        return matApiResponse;
    }

    /**
     * 校验编码
     *
     * @param pid
     * @param code
     * @return
     */
    @GetMapping(value = "/checkCode")
    public MatApiResponse<?> checkCode(@RequestParam(name = "pid", required = false) String pid, @RequestParam(name = "code", required = false) String code) {
        MatApiResponse<?> matApiResponse = sysCategoryRestAPI.checkCode(pid, code);
        return matApiResponse;
    }


    /**
     * 分类字典树控件 加载节点
     *
     * @param pid
     * @param pcode
     * @param condition
     * @return
     */
    @RequestMapping(value = "/loadTreeData", method = RequestMethod.GET)
    public MatApiResponse<List<TreeSelectModelDTO>> loadDict(@RequestParam(name = "pid", required = false) String pid, @RequestParam(name = "pcode", required = false) String pcode, @RequestParam(name = "condition", required = false) String condition) {
        MatApiResponse<List<TreeSelectModelDTO>> matApiResponse = sysCategoryRestAPI.loadDict(pid, pcode, condition);
        return matApiResponse;
    }

    /**
     * 分类字典控件数据回显[表单页面]
     *
     * @param ids
     * @param delNotExist 是否移除不存在的项，默认为true，设为false如果某个key不存在数据库中，则直接返回key本身
     * @return
     */
    @RequestMapping(value = "/loadDictItem", method = RequestMethod.GET)
    public MatApiResponse<List<String>> loadDictItem(@RequestParam(name = "ids") String ids, @RequestParam(name = "delNotExist", required = false, defaultValue = "true") boolean delNotExist) {
        MatApiResponse<List<String>> matApiResponse = sysCategoryRestAPI.loadDictItem(ids, delNotExist);
        return matApiResponse;
    }

    /**
     * [列表页面]加载分类字典数据 用于值的替换
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/loadAllData", method = RequestMethod.GET)
    public MatApiResponse<List<DictModelDTO>> loadAllData(@RequestParam(name = "code", required = true) String code) {
        MatApiResponse<List<DictModelDTO>> matApiResponse = sysCategoryRestAPI.loadAllData(code);
        return matApiResponse;
    }

    /**
     * 根据父级id批量查询子节点
     *
     * @param parentIds
     * @return
     */
    @GetMapping("/getChildListBatch")
    public MatApiResponse getChildListBatch(@RequestParam("parentIds") String parentIds) {
        MatApiResponse matApiResponse = sysCategoryRestAPI.getChildListBatch(parentIds);
        return matApiResponse;
    }


}
